//
//  MineController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/5.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "MineController.h"
#import "MineCell.h"
#import "MyCollectController.h"
#import "LoginController.h"
#import "FeedBackViewController.h"
#import "EditBoxController.h"
#import <SafariServices/SafariServices.h>
@interface MineController () <SFSafariViewControllerDelegate>

@property (nonatomic, strong) NSDictionary * placeDic;

@end

@implementation MineController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviViewHidden:YES];
    [self.view setBackgroundColor:UIColorMakeWithHex(@"#F5F5F5")];
    
    self.placeDic = @{@"0":@[@""],@"1":@[@"我的收藏",@"我的草稿箱"],@"2":@[@"帮助反馈",@"关于我们",@"隐私政策"]};
    
    [self configTableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (void)configTableView
{
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.tableFooterView = [[UIView alloc]init];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.placeDic.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray * arr = self.placeDic[self.placeDic.allKeys[section]];
    return arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 290;
    }else{
        return 50;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    view.backgroundColor = UIColorMakeWithHex(@"#F5F5F5");
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section == 0 ? 0 : 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray * arr = self.placeDic[self.placeDic.allKeys[indexPath.section]];
    if (indexPath.section == 0) {
        MineCell * cell = [MineCell cell0WithTableView:tableView];
        weakify(self);
        if (CLIENT.isLogin) {
            [cell.userImgBtn sd_setImageWithURL:[NSURL URLWithString:[UserDefaults getUserImage]] forState:0];
            [cell.userName setText:[UserDefaults getUserRealName]];
            [cell.placeLabel setText:@"退出"];
        }else{
            [cell.placeLabel setText:@"登录"];
            [cell.userName setText:@"留光用户"];
            [cell.userImgBtn setImage:[UIImage imageNamed:@"用户默认头像"] forState:0];
        }
        [cell.logBtn setAction:^{
            strongify(self);
            if (CLIENT.isLogin == NO) {
                LoginController * login = [[LoginController alloc]initWithNibName:@"LoginController" bundle:nil];
                [self presentViewController:login animated:YES completion:nil];
            }else if(CLIENT.isLogin == YES){
                if ([[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession]) {
                    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"是否退出登录" message:@"" preferredStyle:QMUIAlertControllerStyleAlert];
                    QMUIAlertAction *action = [QMUIAlertAction actionWithTitle:@"退出" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
                        [[UMSocialManager defaultManager] cancelAuthWithPlatform:UMSocialPlatformType_WechatSession completion:^(id result, NSError *error) {
                            [CLIENT loginOut:^{
                            }];
                            [tableView reloadData];
                        }];
                    }];
                    [alertController addCancelAction];
                    [alertController addAction:action];
                    [alertController showWithAnimated:YES];
                }else{
                    [CLIENT loginOut:^{
                        [QMUITips showSucceed:@"退出成功"];
                    }];
                    [tableView reloadData];
                }
            }
        }];
        return cell;
    }else{
        MineCell * cell = [MineCell cell1WithTableView:tableView];
        [cell.cell1LeftImg setImage:[UIImage imageNamed:arr[indexPath.row]]];
        [cell.cell1LeftName setText:arr[indexPath.row]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray * arr = self.placeDic[self.placeDic.allKeys[indexPath.section]];
    if (indexPath.section == 0) {
        
    }else if (indexPath.section == 1) {
        if ([arr[indexPath.row] isEqualToString:@"我的收藏"]) {
            MyCollectController * collect = [[MyCollectController alloc]initWithNibName:@"MyCollectController" bundle:nil];
            [self.navigationController pushViewController:collect animated:YES];
        }else if ([arr[indexPath.row] isEqualToString:@"我的草稿箱"]){
            EditBoxController * edit = [[EditBoxController alloc]initWithNibName:@"EditBoxController" bundle:nil];
            [self.navigationController pushViewController:edit animated:YES];
        }
    }else{
        if ([arr[indexPath.row] isEqualToString:@"帮助反馈"]) {
            FeedBackViewController * collect = [[FeedBackViewController alloc] init];
            [self.navigationController pushViewController:collect animated:YES];
        }else if ([arr[indexPath.row] isEqualToString:@"关于我们"]){
            [self showPricateWithUrl:GUANYUURL];
        }else if ([arr[indexPath.row] isEqualToString:@"隐私政策"]){
            [self showPricateWithUrl:YINSIURL];
        }
    }
}

- (void)showPricateWithUrl:(NSString *)url
{
    SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:url]];
    safariVC.delegate = self;
    [self presentViewController:safariVC animated:YES completion:nil];
}

//加载完成
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
    NSLog(@"加载完成");
}

//点击左上角的done
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
    [controller dismissViewControllerAnimated:YES completion:nil];
}

@end
